package com.jhgsys.internal.accident.service.impl;

import com.jhgsys.internal.accident.entity.AccidentReportRectificationFile;
import com.jhgsys.internal.accident.mapper.AccidentReportRectificationFileMapper;
import com.jhgsys.internal.accident.service.IAccidentReportRectificationFileService;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.system.entity.User;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *  Service实现
 *
 * @author admin
 * @date 2024-09-18 17:23:50
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AccidentReportRectificationFileServiceImpl extends ServiceImpl<AccidentReportRectificationFileMapper, AccidentReportRectificationFile> implements IAccidentReportRectificationFileService {

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-09-18 17:23:50
     * @param request QueryRequest
     * @param accidentReportRectificationFile accidentReportRectificationFile
     * @return IPage<AccidentReportRectificationFile>
    */
    @Override
    public IPage<AccidentReportRectificationFile> findAccidentReportRectificationFilePage(QueryRequest request, AccidentReportRectificationFile accidentReportRectificationFile) {
        LambdaQueryWrapper<AccidentReportRectificationFile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccidentReportRectificationFile::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<AccidentReportRectificationFile> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-09-18 17:23:50
     * @param accidentReportRectificationFile accidentReportRectificationFile
     * @return List<AccidentReportRectificationFile>
     */
    @Override
    public List<AccidentReportRectificationFile> findAccidentReportRectificationFileList(AccidentReportRectificationFile accidentReportRectificationFile) {
	    LambdaQueryWrapper<AccidentReportRectificationFile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AccidentReportRectificationFile::getDeletedFlg, 0);
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author admin
    * @date 2024-09-18 17:23:50
    * @param id
    * @return AccidentReportRectificationFile
    */
    @Override
    public AccidentReportRectificationFile findAccidentReportRectificationFileById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author admin
     * @date 2024-09-18 17:23:50
     * @param accidentReportRectificationFile accidentReportRectificationFile
     */
    @Override
    @Transactional
    public boolean createAccidentReportRectificationFile(AccidentReportRectificationFile accidentReportRectificationFile) {
        return this.save(accidentReportRectificationFile);
    }

    /**
     * 修改
     * @author admin
     * @date 2024-09-18 17:23:50
     * @param accidentReportRectificationFile accidentReportRectificationFile
     */
    @Override
    @Transactional
    public boolean updateAccidentReportRectificationFile(AccidentReportRectificationFile accidentReportRectificationFile) {
        return this.updateById(accidentReportRectificationFile);
    }

    /**
     * 逻辑删除
     * @author admin
     * @date 2024-09-18 17:23:50
     * @param accidentReportRectificationFile accidentReportRectificationFile
     * @param accidentReportRectificationFileIds accidentReportRectificationFileIds
     */
    @Override
    @Transactional
    public boolean deleteAccidentReportRectificationFile(AccidentReportRectificationFile accidentReportRectificationFile,String accidentReportRectificationFileIds) {
        boolean result = false;
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<String> list = Arrays.asList(accidentReportRectificationFileIds.split(StringPool.COMMA));

        accidentReportRectificationFile.setDeletedBy(user.getUserName());
        accidentReportRectificationFile.setDeletedTime(new Date());
        accidentReportRectificationFile.setDeletedFlg(FebsConstant.INVALID);
        int update = this.baseMapper.update(accidentReportRectificationFile,new LambdaQueryWrapper<AccidentReportRectificationFile>().in(AccidentReportRectificationFile::getReportRectificationFileId,list));
        if (update > 0 ){
            result = true;
        }
        return result;
	}

    /**
    * 物理删除
    * @author admin
    * @date 2024-09-18 17:23:50
    * @param accidentReportRectificationFileIds accidentReportRectificationFileIds
    */
    @Override
    @Transactional
    public boolean physicallyDeleteAccidentReportRectificationFile(String accidentReportRectificationFileIds) {
        List<String> list = Arrays.asList(accidentReportRectificationFileIds.split(StringPool.COMMA));
        LambdaQueryWrapper<AccidentReportRectificationFile> wapper = new LambdaQueryWrapper<>();
        wapper.in(AccidentReportRectificationFile::getReportRectificationFileId,list);
        return this.remove(wapper);
    }

    @Override
    public void deleteByReportRectificationId(String reportRectificationId) {
        LambdaQueryWrapper<AccidentReportRectificationFile> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AccidentReportRectificationFile::getReportRectificationId,reportRectificationId);
        this.remove(wrapper);
    }
}
